﻿/*
*VMukti -- An open source video conferencing platform.
*
* Copyright (C) 2007 - 2008, VMukti Solutions Pvt. Ltd.
*
* Hardik Sanghvi <hardik@vmukti.com>
*
* See http://www.vmukti.com for more information about
* the VMukti project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CRM.DataAccess
{
    public class ClsQuestionAnsDataService : ClsDataServiceBase
    {

        public ClsQuestionAnsDataService() : base() { }

        ////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///	Creates a new DataService and specifies a transaction with
        ///	which to operate
        /// </summary>
        ////////////////////////////////////////////////////////////////////////
        public ClsQuestionAnsDataService(IDbTransaction txn) : base(txn) { }


        public DataSet Script_GetAll()
        {
            try
            {
                return ExecuteDataSet("Select * from vScript where IsDeleted=0;", CommandType.Text, null);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "ClsQuestionAnsDataService()--:--ClsQuestionAnsDataService.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        public DataSet Question_GetAll(int ScriptID)
        {
            try
            {
                return ExecuteDataSet("Select * from Question where ScriptID=" + ScriptID.ToString() + ";", CommandType.Text, null);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Question_GetAll()--:--ClsQuestionAnsDataService.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        public DataSet Options_GetAll(int QuesID)
        {
            try
            {
                return ExecuteDataSet("Select * from QuestionOptions where QuestionID=" + QuesID.ToString() + ";", CommandType.Text, null);
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Options_GetAll()--:--ClsQuestionAnsDataService.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        public DataSet Question_GetByID(int QueID)
        {
            try
            {
                return ExecuteDataSet("spGQuestion", CommandType.StoredProcedure, CreateParameter("@pID", SqlDbType.Int, QueID));
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Question_GetByID()--:--ClsQuestionAnsDataService.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        public int Question_Save(int ID, string Header, string Text, string Desc, string Category, int ScriptID)
        {
            try
            {
                SqlCommand cmd;
                ExecuteNonQuery(out cmd, "spAEQuestion",
                    CreateParameter("@pID", SqlDbType.Int, ID),
                    CreateParameter("@pQuestionName", SqlDbType.NVarChar, Header, 100),
                    CreateParameter("@pQuestionText", SqlDbType.NVarChar, Text, 255),
                    CreateParameter("@pDescription", SqlDbType.NVarChar, Desc, 100),
                    CreateParameter("@pCategory", SqlDbType.NVarChar, Category, 50),
                    CreateParameter("@pScriptID", SqlDbType.BigInt, ScriptID),
                    CreateParameter("@pReturnMaxId", SqlDbType.BigInt, -1, ParameterDirection.Output));

                cmd.Dispose();

                return (int.Parse(cmd.Parameters["@pReturnMaxId"].Value.ToString()));
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Question_Save()--:--ClsQuestionAnsDataService.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);
                return null;
            }
        }

        public void Options_Delete(int QuestionID)
        {
            try
            {
                SqlCommand cmdDeleteJoin;
                ExecuteNonQuery(out cmdDeleteJoin, "spDQuestionOptions",
                    CreateParameter("@pQuestionID", SqlDbType.Int, QuestionID));
                cmdDeleteJoin.Dispose();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Options_Delete()--:--ClsQuestionAnsDataService.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void Options_Save(int ID, string Option, string Description, int QuestionID, int ActionQuestionID)
        {
            try
            {
                if (ActionQuestionID == null)
                {
                    ActionQuestionID = QuestionID;
                }

                SqlCommand cmd;
                ExecuteNonQuery(out cmd, "spAEQuestionOptions",
                    CreateParameter("@pID", SqlDbType.Int, ID),
                    CreateParameter("@pOptions", SqlDbType.NVarChar, Option, 100),
                    CreateParameter("@pDescription", SqlDbType.NVarChar, Description, 100),
                    CreateParameter("@pQuestionID", SqlDbType.BigInt, QuestionID),
                    CreateParameter("@pActionQueueID", SqlDbType.BigInt, ActionQuestionID));
                cmd.Dispose();
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Options_Save()--:--ClsQuestionAnsDataService.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

        public void Question_Delete(int ID)
        {
            try
            {
                ExecuteNonQuery("spDQuestionOptions", CreateParameter("@pQuestionID", SqlDbType.Int, ID));
                ExecuteNonQuery("spDQuestion", CreateParameter("@pID", SqlDbType.Int, ID));
            }
            catch (Exception ex)
            {
                ex.Data.Add("My Key", "Question_Delete()--:--ClsQuestionAnsDataService.cs--:--" + ex.Message + " :--:--");
                ClsException.LogError(ex);
                ClsException.WriteToErrorLogFile(ex);

            }
        }

    }
}
